package My.project.Ui.Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import My.project.Entity.People;
import My.project.Ui.R;
import My.project.Util.Connector;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * This fragment is used to display all friends of the user.
 * @author Bell
 *
 */
public class FriendFragment extends ListFragment {

	private int mCurCheckPosition = 0;
	private List<People> friendList = new ArrayList<People>();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        SimpleAdapter adapter = new SimpleAdapter(
        		this.getActivity(), this.getData(), R.layout.vlist, 
        		new String[]{"title","info","img"}, 
        		new int[]{R.id.title,R.id.info,R.id.img});
        setListAdapter(adapter);

        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    }
    
    private List<Map<String, Object>> getData() {
    	// generate people list
    	Connector con = new Connector();
    	friendList = con.getPeopleList();
    	
    	// generate list to be displayed
        List<Map<String, Object>> list =
        		new ArrayList<Map<String, Object>>();
 
        for (int i = 0; i < friendList.size(); i++) {
        	if (!friendList.get(i).isFriend()) continue;
        	Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", friendList.get(i).getNick());
            map.put("info", friendList.get(i).getDes());
            map.put("img", R.drawable.social_person);
            list.add(map);
        }
        return list;
    }
}
